package com.mall.common.service;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.mall.untils.Page;
import com.mall.untils.StringUtil;

/**
 * 地区底层业务
 * @ClassName: areaService
 * @Description: 
 * @author:wangwenyue wangwenyue@ide365.com
 * @date 2016年7月8日 上午11:10:40
 */
@Service
public class AreaService {
	

	Logger logger=LoggerFactory.getLogger(AreaService.class);
	@Resource
	private JdbcTemplate jdbcTemplate;
	
	/**
	 * 分页
	 * @param paraMap
	 * @return
	 */
	public Page area_list(Map<String, Object> paraMap){
		Page page = null;
		List<Map<String, Object>> queryForList = null;
		int pageNo = MapUtils.getIntValue(paraMap, "pageNo");
		String name = MapUtils.getString(paraMap, "name");
		int pageSize = 10;
		try {
			if (pageNo == 0) {
				pageNo = 1;
			}
			pageNo = 2;
			StringBuffer sqlCount = new StringBuffer("select count(*) from t_e_area where 1=1 ");
			if (StringUtils.isNotEmpty(name)) {
				sqlCount.append("and name like ? ");
			}
			Integer total = jdbcTemplate.queryForObject(sqlCount.toString(), Integer.class);
			int startIndex = Page.getStartOfPage(pageNo, pageSize);
			StringBuffer sql = new StringBuffer("select * from t_e_area where 1=1 ");

			if (StringUtils.isNotEmpty(name)) {
				sql.append("and name like ? ");
			}

			String sqlbf = sql.append(" order by id desc limit " + startIndex + "," + pageSize).toString();
			if (StringUtils.isNotEmpty(name)) {
				queryForList = jdbcTemplate.queryForList(sqlbf, "%"+name+"%");
			}
			queryForList = jdbcTemplate.queryForList(sqlbf);
			page = new Page(startIndex, total, pageSize, queryForList);
			return page;
		} catch (Exception e) {
			e.printStackTrace();
			logger.info(e.toString());
			return null;
		}
		
	}
	
	
	/**
	 * 添加地区
	 * @param paraMap
	 * @return
	 */
	public int area_add(Map<String, Object> paraMap) {
		String e_name = MapUtils.getString(paraMap, "e_name");
		int e_type = MapUtils.getIntValue(paraMap, "e_type");//1：省，2：市，3：区
		String parent_id = MapUtils.getString(paraMap, "parent_id");
		String sql = "insert into t_e_area(e_name,e_type,parent_id,create_time,update_time) values(?,?,?,now(),now())";
		try {
			int update = jdbcTemplate.update(sql, StringUtil.getByteToString(e_name), e_type, parent_id);
			return update;
		} catch (Exception e) {
			logger.info(e.toString());
			return 0;
		}
	}


	/**
	 * 删除
	 * @param paraMap
	 * @return
	 */
	public int area_rem(Map<String, Object> paraMap) {
		int id = MapUtils.getIntValue(paraMap, "id");
		try {
			String sql = "delete from t_e_area where id=?";
			return jdbcTemplate.update(sql, id);
		} catch (Exception e) {
			logger.info(e.toString());
			return 0;
		}
	}


	/**
	 * 更新
	 * @param paraMap
	 * @return
	 */
	public int area_update(Map<String, Object> paraMap) {
		String e_name = MapUtils.getString(paraMap, "e_name");
		int e_type = MapUtils.getIntValue(paraMap, "e_type");//1：省，2：市，3：区
		String parent_id = MapUtils.getString(paraMap, "parent_id");
		String sql = "udpate  t_e_area set e_name=?, e_type=?,parent_id=?,update_time) values(?,?,?,now())";
		try {
			int update = jdbcTemplate.update(sql, StringUtil.getByteToString(e_name), e_type, parent_id);
			return update;
		} catch (Exception e) {
			logger.info(e.toString());
			return 0;
		}
	}
	

}
